{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "·{\"cells\": [\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": 1,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"import os\\n\",\n",
    "    \"import re\\n\",\n",
    "    \"from pyvis.network import Network\\n\",\n",
    "    \"from pyltp import Segmentor,Postagger,Parser,SementicRoleLabeller\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": 2,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def parser_main(sentence):\\n\",\n",
    "    \"    LTP_DIR=\\\"C:/Users/Administrator/Desktop/ltp_data_v3.4.0\\\"\\n\",\n",
    "    \"    segmentor=Segmentor(os.path.join(LTP_DIR,\\\"cws.model\\\"))\\n\",\n",
    "    \"    postagger=Postagger(os.path.join(LTP_DIR,\\\"pos.model\\\"))\\n\",\n",
    "    \"    parser=Parser(os.path.join(LTP_DIR,\\\"parser.model\\\"))\\n\",\n",
    "    \"    labeller=SementicRoleLabeller(os.path.join(LTP_DIR,\\\"pisrl_win.model\\\"))\\n\",\n",
    "    \"    words=list(segmentor.segment(sentence))\\n\",\n",
    "    \"    postags=list(postagger.postag(words))\\n\",\n",
    "    \"    arcs=parser.parse(words,postags)\\n\",\n",
    "    \"    roles=labeller.label(words,postags,arcs)\\n\",\n",
    "    \"    roles_dict={key:{sub_key:[sub_key,*value]for sub_key,value in sub_data}for key,sub_data in roles}\\n\",\n",
    "    \"    return words,postags,arcs,roles_dict\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"text=\\\"苏轼是宋朝的著名文学家，黄庭坚是苏轼的好朋友。苏轼擅长写词，而黄庭坚擅长写诗。黄庭坚游览黄州，并赞叹黄山之美。黄庭坚探望苏轼，并一起吟诗作对。\\\"\\n\",\n",
    "    \"words,postags,arcs,roles_dict=parser_main(text)\\n\",\n",
    "    \"print(\\\"分词结果:\\\",words)\\n\",\n",
    "    \"print(\\\"语义角色标注结果:\\\",roles_dict)\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": 11,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def ruler(words,postags,roles_dict,role_index):\\n\",\n",
    "    \"    v=words[role_index]\\n\",\n",
    "    \"    role_info=roles_dict[role_index]\\n\",\n",
    "    \"    if'A0'in role_info.keys()and'A1'in role_info.keys():\\n\",\n",
    "    \"        s=''.join([words[word_index]for word_index in range(role_info['A0'][1],role_info['A0'][2]+1)if postags[word_index][0]not in['w','u','x']and words[word_index]])\\n\",\n",
    "    \"        if s and o:\\n\",\n",
    "    \"            return'1',[s,v,o]\\n\",\n",
    "    \"    return'4',[]\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def triples_main(text):\\n\",\n",
    "    \"    sentences=[sentence for sentence in re.split(r'[？?！!。；;：:\\\\n\\\\r]',text)if sentence]\\n\",\n",
    "    \"    svos=[]\\n\",\n",
    "    \"    for index,sentence in enumerate(sentences):\\n\",\n",
    "    \"        words,postags,arcs,roles_dict=parser_main(sentence)\\n\",\n",
    "    \"        for index in range(len(postags)):\\n\",\n",
    "    \"            if index in roles_dict:\\n\",\n",
    "    \"                flag,triple=ruler(words,postags,roles_dict,index)\\n\",\n",
    "    \"                if flag=='1':\\n\",\n",
    "    \"                    svos.append(triple)\\n\",\n",
    "    \"    return svos\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"svos=triples_main(text)\\n\",\n",
    "    \"print(\\\"文本的三元组:svos={0}\\\".format(svos))\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def create_node(net,subs1,subs2):\\n\",\n",
    "    \"    if len(subs1)!=0:\\n\",\n",
    "    \"        for i in range(len(subs1)):\\n\",\n",
    "    \"            net.add_node(i,label=subs1[i],color=\\\"bule\\\")\\n\",\n",
    "    \"    if len(subs2)!=0:\\n\",\n",
    "    \"        for i in range(len(subs2)):\\n\",\n",
    "    \"            net.add_node(1000+i,label=subs2[i],color=\\\"green\\\")\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def create_node_id_dic(net):\\n\",\n",
    "    \"    dic_node_id={}\\n\",\n",
    "    \"    for i in net.node_ids:\\n\",\n",
    "    \"        dic_node_id[str(net.node_map[i][\\\"label\\\"])]=i\\n\",\n",
    "    \"    return dic_node_id\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def create_network(net,kg_list,node_id_dic):\\n\",\n",
    "    \"    for m in range(len(kg_list)):\\n\",\n",
    "    \"        try:\\n\",\n",
    "    \"            net.add_edge(node_id_dic[kg_list[m][0]],node_id_dic[kg_list[m][2]],label=kg_list[m][1],color=\\\"red\\\",width=2)\\n\",\n",
    "    \"        except AttributeError as e:\\n\",\n",
    "    \"            print(e,m)\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"net=Network(notebook=True,directed=True)\\n\",\n",
    "    \"subs1=list(set(sublist[0]for sublist in svos))\\n\",\n",
    "    \"subs2=list(set(sublist[2]for sublist in svos))\\n\",\n",
    "    \"create_node(net,subs1,subs2)\\n\",\n",
    "    \"dic_node_id=create_node_id_dic(net)\\n\",\n",
    "    \"create_network(net,svos,dic_node_id)\\n\",\n",
    "    \"net.show(\\\"my_network.html\\\")\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"def find_similar_semantics(net,node_id_dic,target_node):\\n\",\n",
    "    \"    target_node_id=node_id_dic[target_node]\\n\",\n",
    "    \"    adjacent_nodes=net.neighbors(target_node_id)\\n\",\n",
    "    \"    similar_semantics=[node_id_dic_inverse[node_id]for node_id in adjacent_nodes]\\n\",\n",
    "    \"    return similar_semantics\"\n",
    "   ]\n",
    "  },\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"execution_count\": null,\n",
    "   \"metadata\": {},\n",
    "   \"outputs\": [],\n",
    "   \"source\": [\n",
    "    \"node_id_dic_inverse={v:k for k,v in dic_node_id.items()}\\n\",\n",
    "    \"similar=find_similar_semantics(net,dic_node_id,'苏轼')\\n\",\n",
    "    \"print(\\\"与目标词'苏轼'语义相关的词:\\\",similar)\"\n",
    "   ]\n",
    "  }\n",
    " ],\n",
    " \"metadata\": {\n",
    "  \"kernelspec\": {\n",
    "   \"display_name\": \"Python 3\",\n",
    "   \"language\": \"python\",\n",
    "   \"name\": \"python3\"\n",
    "  },\n",
    "  \"language_info\": {\n",
    "   \"codemirror_mode\": {\n",
    "    \"name\": \"ipython\",\n",
    "    \"version\": 3\n",
    "   },\n",
    "   \"file_extension\": \".py\",\n",
    "   \"mimetype\": \"text/x-python\",\n",
    "   \"name\": \"python\",\n",
    "   \"nbconvert_exporter\": \"python\",\n",
    "   \"pygments_lexer\": \"ipython3\",\n",
    "   \"version\": \"3.7.0\"\n",
    "  }\n",
    " },\n",
    " \"nbformat\": 4,\n",
    " \"nbformat_minor\": 2\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
